/** 开启页面标题守卫 */
import type { Router } from 'vue-router'
import { useTitle } from '@vueuse/core'
import { TITLE } from '../../settings'
import useMicroStore from '../../store/micro'
import { isMicro } from '../../utils'

export function createPageTitleGuard(router: Router, defaultTitle?: string) {
  const microStore = useMicroStore()

  router.afterEach((to) => {
    if (microStore.isMicro) {
      microStore.getProps?.setTitle?.((to.meta.title as string) || defaultTitle || TITLE)
    }
    else {
      if (!isMicro(to.path))
        useTitle((to.meta.title as string) || defaultTitle || TITLE)
    }
  })
}
